/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.transaction;

import java.lang.reflect.Method;

import org.openinsula.vulcano.core.command.Command;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
 * @author Eduardo Rebola
 *
 */
@Component("transactionInvoker")
public class TransactionInvokerImpl implements TransactionInvoker {
	private final TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(true);

	private PlatformTransactionManager transactionManager;

	@Autowired(required = false)
	public void setTransactionManager(final @Qualifier("transactionManager") PlatformTransactionManager transactionManager) {
		Assert.notNull(transactionManager, "PlatformTransactionManager required!");
		this.transactionManager = transactionManager;
	}
	
	@Override
	public PlatformTransactionManager getDefaultTransactionManager() {
		return transactionManager;
	}
	
	@Override
	public TransactionDefinition getTransactionDefinition(final Command<?> command) {
		Method executeMethod = ReflectionUtils.findMethod(command.getClass(), "execute");

		if (executeMethod == null) {
			throw new IllegalArgumentException("Unable to find Command's execute method");
		}
		
		TransactionAttribute transactionAttribute = transactionAttributeSource.getTransactionAttribute(executeMethod, command.getClass());
		return transactionAttribute == null ? new DefaultTransactionDefinition() : transactionAttribute;
	}

	@Override
	public <T> T invoke(final Command<T> command) {
		return invoke(command, getDefaultTransactionManager());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T invoke(final Command<T> command, final PlatformTransactionManager transactionManager) {
		Assert.notNull(command, "Command required!");
		Assert.notNull(transactionManager, "PlatformTransactionManager missing!");
		
		TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager, getTransactionDefinition(command));

		T result = (T) transactionTemplate.execute(new TransactionCallback() {

			@Override
			public Object doInTransaction(final TransactionStatus status) {
				return command.execute();
			}

		});

		return result;
	}

}
